package sg.edu.nus.iss.billing.gui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import sg.edu.nus.iss.billing.controller.UserController;
import sg.edu.nus.iss.billing.exception.ControllerException;
import sg.edu.nus.iss.billing.exception.ValidationException;
import sg.edu.nus.iss.billing.gui.component.ErrorDialog;
import sg.edu.nus.iss.billing.gui.component.ValidateJTextField;
import sg.edu.nus.iss.billing.localization.MessageKeyManager;
import sg.edu.nus.iss.billing.privilege.AdminRole;
import sg.edu.nus.iss.billing.validation.FormValidator;
import sg.edu.nus.iss.billing.validation.ValidationRule;

public class LoginPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4975528804578330589L;
	private static final String EN_LOCALE = "English (en_US)";
	private static final String PT_LOCALE = "Portuguese (pt_BR)";
	private static final String ID_LOCALE = "Indonesian (id_ID)";

	private ValidateJTextField usernameTxt;
	private JPasswordField passwordTxt;
	private JLabel lblUsername;
	private JLabel lblPassword;
	private JLabel lblLanguage;
	private JButton btnLogin;
	private UserController userController;
	private JComboBox comboBoxLanguage;
	private MainFrame frame;

	private class LocaleChange implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String selectedLocale = (String) comboBoxLanguage.getSelectedItem();
			if (EN_LOCALE.equals(selectedLocale)) {
				MessageKeyManager.setLocale(MessageKeyManager.EN_LOCALE);
				MessageKeyManager.reload();
			} else if (PT_LOCALE.equals(selectedLocale)) {
				MessageKeyManager.setLocale(MessageKeyManager.PT_LOCALE);
				MessageKeyManager.reload();
			} else if (ID_LOCALE.equals(selectedLocale)) {
				MessageKeyManager.setLocale(MessageKeyManager.ID_LOCALE);
				MessageKeyManager.reload();
			}
			lblUsername.setText(MessageKeyManager.getMessage("label.username"));
			lblPassword.setText(MessageKeyManager.getMessage("label.password"));
			lblLanguage.setText(MessageKeyManager.getMessage("label.language"));
			btnLogin.setText(MessageKeyManager.getMessage("button.login"));
			frame.autoResize();
		}

	}

	/**
	 * Create the panel.
	 */
	public LoginPanel(MainFrame mainFrame) {
		this.frame = mainFrame;
		this.userController = new UserController();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 24, 24, 24, 24, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		lblUsername = new JLabel(MessageKeyManager.getMessage("label.username"));
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.anchor = GridBagConstraints.EAST;
		gbc_lblUsername.insets = new Insets(5, 5, 5, 5);
		gbc_lblUsername.gridx = 0;
		gbc_lblUsername.gridy = 0;
		add(lblUsername, gbc_lblUsername);

		this.usernameTxt = new ValidateJTextField(
				MessageKeyManager.getMessage("label.username"),
				ValidationRule.NOT_NULL);
		this.usernameTxt.setColumns(10);
		GridBagConstraints gbc_usernameTxt = new GridBagConstraints();
		gbc_usernameTxt.fill = GridBagConstraints.HORIZONTAL;
		gbc_usernameTxt.insets = new Insets(5, 5, 5, 5);
		gbc_usernameTxt.gridx = 1;
		gbc_usernameTxt.gridy = 0;
		add(this.usernameTxt, gbc_usernameTxt);

		lblPassword = new JLabel(MessageKeyManager.getMessage("label.password"));
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(5, 5, 5, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 1;
		add(lblPassword, gbc_lblPassword);

		this.passwordTxt = new JPasswordField();
		this.passwordTxt.setColumns(10);
		GridBagConstraints gbc_passwordTxt = new GridBagConstraints();
		gbc_passwordTxt.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordTxt.insets = new Insets(5, 5, 5, 5);
		gbc_passwordTxt.gridx = 1;
		gbc_passwordTxt.gridy = 1;
		add(this.passwordTxt, gbc_passwordTxt);

		lblLanguage = new JLabel(MessageKeyManager.getMessage("label.language"));
		lblLanguage.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLanguage.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblLanguage = new GridBagConstraints();
		gbc_lblLanguage.anchor = GridBagConstraints.EAST;
		gbc_lblLanguage.insets = new Insets(5, 5, 5, 5);
		gbc_lblLanguage.gridx = 0;
		gbc_lblLanguage.gridy = 2;
		add(lblLanguage, gbc_lblLanguage);

		comboBoxLanguage = new JComboBox();
		comboBoxLanguage
				.setModel(new DefaultComboBoxModel(new String[] {
						"English (en_US)", "Portuguese (pt_BR)",
						"Indonesian (id_ID)" }));
		GridBagConstraints gbc_comboBoxLanguage = new GridBagConstraints();
		gbc_comboBoxLanguage.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBoxLanguage.insets = new Insets(5, 5, 5, 5);
		gbc_comboBoxLanguage.gridx = 1;
		gbc_comboBoxLanguage.gridy = 2;
		add(comboBoxLanguage, gbc_comboBoxLanguage);
		comboBoxLanguage.addActionListener(new LocaleChange());

		btnLogin = new JButton(MessageKeyManager.getMessage("button.login"));
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				boolean state = false;
				try {
					String username = usernameTxt.getValidatedText();
					String password = new String(passwordTxt.getPassword());
					FormValidator.validate(
							MessageKeyManager.getMessage("label.password"),
							password, ValidationRule.NOT_NULL);
					state = userController.authenticateUser(username, password);
					if (state == true) {
						frame.addUser(userController.getUserByName(username));
					}
				} catch (ValidationException ve) {
					ErrorDialog.showValidationErrorMessageDialog(ve);
				} catch (ControllerException ce) {
					ErrorDialog.showControllerErrorMessageDialog(ce);
				} catch (Exception ex) {
					ErrorDialog.showUnhandledErrorMessageDialog(ex);
				}
			}
		});
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnLogin.anchor = GridBagConstraints.NORTH;
		gbc_btnLogin.insets = new Insets(5, 5, 0, 5);
		gbc_btnLogin.gridx = 1;
		gbc_btnLogin.gridy = 3;
		add(btnLogin, gbc_btnLogin);
	}
}